package com.weatherapp.simple.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.weatherapp.simple.R;
import com.weatherapp.simple.model.DailyWeather;
import java.util.List;

public class DailyWeatherAdapter extends RecyclerView.Adapter<DailyWeatherAdapter.ViewHolder> {
    
    private List<DailyWeather> dailyWeatherList;

    public DailyWeatherAdapter(List<DailyWeather> dailyWeatherList) {
        this.dailyWeatherList = dailyWeatherList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_daily_weather, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DailyWeather weather = dailyWeatherList.get(position);
        holder.tvDayDate.setText(weather.getDayOfWeek() + " " + weather.getDate());
        holder.tvWeatherIcon.setText(weather.getWeatherIcon());
        holder.tvWeatherDesc.setText(weather.getWeatherDescription());
        holder.tvTemperatureRange.setText(weather.getMaxTemperature() + "°C/" + weather.getMinTemperature() + "°C");
    }

    @Override
    public int getItemCount() {
        return dailyWeatherList != null ? dailyWeatherList.size() : 0;
    }

    public void updateData(List<DailyWeather> newData) {
        this.dailyWeatherList = newData;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDayDate;
        TextView tvWeatherIcon;
        TextView tvWeatherDesc;
        TextView tvTemperatureRange;

        ViewHolder(View itemView) {
            super(itemView);
            tvDayDate = itemView.findViewById(R.id.tv_day_date);
            tvWeatherIcon = itemView.findViewById(R.id.tv_weather_icon);
            tvWeatherDesc = itemView.findViewById(R.id.tv_weather_desc);
            tvTemperatureRange = itemView.findViewById(R.id.tv_temperature_range);
        }
    }
}